package com.ruoyi.job.task;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component("yingShiTokenTask")
public class YingShiTokenTask {

    @Autowired
    private RedisService redisService;

    /**
     * 获取推送token
     */
    public void getTokenTask(String appkey, String appSecret, String baseUrl){

        String url = baseUrl +"?appKey="+ appkey +"&appSecret=" + appSecret;
        HttpRequest httpRequest= HttpUtil.createPost(url).header("content-type","application/x-www-form-urlencoded");
        String resultStr = httpRequest.execute().body();
        //String resultStr = HttpRequest.post(url).header("content-type","application/x-www-form-urlencoded").form(params).execute().body();
        JSONObject result = JSONUtil.parseObj(resultStr);
        if (result.getStr("code").equals("200")){
            JSONObject data = (JSONObject) result.get("data");
            String token = data.getStr("accessToken");
            System.out.println("yingshi: " + token);
            String expireTime = data.getStr("expireTime");
            redisService.setCacheObject(appkey, token, Long.parseLong(expireTime), TimeUnit.MILLISECONDS);
        }

    }
}
